/*
 * @Author: OreoWang
 * @Email: ihc523@163.com
 * @Date: 2022-04-25 16:48:39
 * @LastEditors: OreoWang
 * @LastEditTime: 2022-04-27 22:51:48
 * @Description: 
 */

import { SkeletalAnimation } from 'cc';
import { AnimationComponent } from 'cc';
import { SystemEventType } from 'cc';
import { SystemEvent } from 'cc';
import { _decorator, Component, Node } from 'cc';
import bt from '../../script/behavior-runtime/main';
import { Target } from '../../toolkit/joystick/UIJoystick';
const { ccclass, property } = _decorator;

@ccclass('AIPlayer')
export class AIPlayer extends Component {
    // @property(Node)
    // camera: Node = null;
    
    @property(SkeletalAnimation)
    anim: SkeletalAnimation = null;
    
    _animName: string = "";

    onLoad(){
        Target.on(SystemEvent.EventType.TOUCH_START, this.onTouchStart, this);
        Target.on(SystemEvent.EventType.TOUCH_MOVE, this.onTouchMove, this);
        Target.on(SystemEvent.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    start () {
        this._playAnim("idle");
    }

    onTouchStart() {
        // this._playAnim("run");
    }
    onTouchMove() {
        this._playAnim("run");
    }
    onTouchEnd() {
        this._playAnim("idle");
    }
    
    onIdle(){
        return bt.BehaviorStatus.Running;
    }

    beAttacked(){
        // this._playAnim("hitSmall");
        this._playAnim("hitBig");
        this.anim.once(AnimationComponent.EventType.LASTFRAME, ()=>{
            this._playAnim("idle");
        })
    }

    update(delta: number){
        
    }

    _playAnim(name: string){
        if(this._animName == name){
            return;
        }

        this._animName = name;
        this.anim.play(name);
        // this.anim.once(AnimationComponent.EventType.FINISHED, ()=>{
        //     this._playAnim("idle");
        // })
    }
}
